<?

class mailer
{
	var $header = "";
	var $footer = "";
	var $powered = SITEURL;
	var $from = FROM;
	var $admin_email = ADMINEMAIL;
	var $business_id = 0;
	var $business;
	
	function mailer($business_id)
	{
		$query = "select * from business_info where business_id='$business_id'";
		$res = $this->query($query);
		$row = mysql_fetch_array($res);
		$this->business = $row;
		$this->initVars($row);
	}
	
	function initVars($row)
	{
		$this->business_id = $row['business_id'];
		$this->header = $row['companyname'];
		$this->footer = "Copyright &copy; ".$row['companyname'].". All rights Reserved.";
		$this->admin = $row['email'];
	}
	
	
	function query($query,$extra="")
	{
		$result = mysql_query($query) or die("Query execution error : ".mysql_error()."<br> Query : $query <br> Query Type : $extra");
		return $result;
	}
	
	function sendmail($to, $subject, $body)
	{
		$from = $this->from;
		// Always set content-type when sending HTML email
		$headers = "MIME-Version: 1.0" . "\r\n";
		$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
		
		// More headers
		$headers .= 'From: <'.$this->from.'>' . "\r\n";
		
		if($this->admin!="") {
			$headers .= 'Bcc: <'.$this->admin.'>' . "\r\n";
		}

		if(PRODUCTION == 1) {
			mail($to,$subject,$body,$headers);
		}
	}
	
	function getbody($body)
	{
		$content ='
		<style type="text/css">
<!--
.style3 {font-family: Arial, Helvetica, sans-serif}
.style4 {
	font-size: 10px;
	font-family: Arial, Helvetica, sans-serif;
	color: #666666;
}
table tr td {
	font-size: 12px;
	font-family: Arial, Helvetica, sans-serif;
}
-->
</style>
<table width="600" border="0" cellpadding="5" cellspacing="5" bgcolor="#FFFFFF">
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td colspan="2"><h2 class="style3">'.$this->header.'</h2></td>
  </tr>

  <tr>
    <td colspan="2">'.$body.'</td>
  </tr>
  <tr>
    <td colspan="2">&nbsp;</td>
  </tr>
  <tr>
    <td colspan="2"><span class="style4">'.$this->footer.'. Powered by <a href="'.$this->powered.'">'.$this->powered.'</a> </span></td>
  </tr>
</table>';
return $content;
	}
	
	function forgotStaffPassword($email)
	{
		$query = "select st.login,st.password,bi.business_id,bi.companyname,bi.email from staff as st 
				  left join business_info as bi on st.business_id = bi.business_id
				  where st.email='$email' limit 1";
		$res = $this->query($query);
		$row = mysql_fetch_array($res);
		if($row['login']!='' && $row['password']!='') {
		     
			 $this->initVars($row);
			 
			 $body = "Your password information retrived is as follows :- <br/><br/>
			 		  Username : ".$row['login']."<br/>
					  Password : ".$row['password']."<br/><br/>
					  Please click <a href='".$this->powered."/index1.php'>here</a> to login ";
					  
			 $body = $this->getBody($body);
			 
			 $this->sendMail($email,'Your password for'.$row['companyname'],$body);
		}

	}
	
	function forgotAdminPassword($email)
	{
		$query = "select ai.username,ai.password,bi.business_id,bi.companyname,bi.email from administrator_info as ai 
				  left join business_info as bi on ai.business_id = bi.business_id
				  where bi.email='$email' limit 1";
		$res = $this->query($query);
		$row = mysql_fetch_array($res);
		if($row['username']!='' && $row['password']!='') {
		     
			 $this->initVars($row);
			 
			 $body = "Your password information retrived is as follows :- <br/><br/>
			 		  Username : ".$row['username']."<br/>
					  Password : ".$row['password']."<br/><br/>
					  Please click <a href='".$this->powered."/index1.php'>here</a> to login ";
					  
			 $body = $this->getBody($body);
			 
			 $this->sendMail($email,'Your password for'.$row['companyname'],$body);
		}
	}
	
	function forgotCustomerPassword($email)
	{
		$query = "select * from customer_info where email='$email' and business_id='$this->business_id'";
		$res = $this->query($query);
		$row = mysql_fetch_array($res);			 
		if($row['username']!='' && $row['password']!='') {
			$body = "Your password information retrived is as follows :- <br/><br/>
					 Username : ".$row['username']."<br/>
					 Password : ".$row['password']."<br/><br/>
					 Please click <a href='".$this->powered."/customer-login.php?business_id=".$this->business_id."'>here</a> to login ";
			$body = $this->getBody($body);
			$this->sendMail($row['email'],'Your password for '.$this->header,$body);
		}
		
		
	}
	
	function createCustomer($customer_id)
	{
		$query = "select email,username,password from customer_info where customer_id='$customer_id'";
		$res = $this->query($query);
		$row = mysql_fetch_array($res);			 
		if($row['username']!='' && $row['password']!='') {
			$body = "our new account has been created successfully. <br/>
					 Please use the following details to login :-<br/>
					 Username : ".$row['username']."<br/>
					 Password : ".$row['password']."<br/><br/>
					 Site URL :  <a href='".$this->powered."/customer-login.php?business_id=".$this->business_id."'>".$this->powered."/customer-login.php?business_id=".$this->business_id."</a> ";
			$body = $this->getBody($body);
			$this->admin = $this->admin_email;
			$this->sendMail($row['email'],'Welcome to '.$this->header,$body);
		}
		
	}
	
	
	function createAppointment($appt_id, $update=0)
	{
		$query = "select app.*,st.first_name,st.last_name,st.email as staffemail,sr.title,sts.description,
				  cus.firstname,cus.lastname,cus.email as cusemail
				  from finalize_appointment as app
				  left join customer_info as cus on cus.customer_id = app.customer_id
				  left join services as sr on sr.service_id = app.service_id
				  left join appointment_status_type as sts on sts.as_id = app.apptstatus
				  left join staff as st on st.staff_id = app.staff_id
				  where app.appt_id='$appt_id' limit 1";
		$res = $this->query($query);
		$row = mysql_fetch_object($res);			 
		
		if($row->appt_id!='') {
			$date = date("d-M-y h:i",$row->starttime)." - " . date("h:i",$row->endtime);
			$cusbody = "New appointment has been created. <br/><br/>
					 <table width=100% cellpadding='2' cellspacing='2'>
						<tr>
							<td width='20%'>Date</td>
							<td>: $date</td>
						</tr>
						<tr>
							<td>Service</td>
							<td>: $row->title</td>
						</tr>
						<tr>
							<td>Assigned to</td>
							<td>: $row->first_name $row->last_name</td>
						</tr>
						<tr>
							<td>Status</td>
							<td><b>: $row->description</b></td>
						</tr>
							<tr>
							<td>Customer Instructions</td>
							<td>: $row->specialinstruction</td>
						</tr>

					 </table>
				
					 Please use the following url to view your appontment.   <br/><a href='".$this->powered."/customer-login.php?business_id=".$this->business_id."'>".$this->powered."/customer-login.php?business_id=".$this->business_id."</a> ";

			$staffbody = "New appointment has been created for you <br/><br/>
					 <table width=100% cellpadding='2' cellspacing='2'>
						<tr>
							<td width='20%'>Date</td>
							<td>: $date</td>
						</tr>
						<tr>
							<td>Service</td>
							<td>: $row->title</td>
						</tr>
						<tr>
							<td>Customer </td>
							<td>: $row->firstname $row->lastname</td>
						</tr>
						<tr>
							<td>Status</td>
							<td><b>: $row->description</b></td>
						</tr>
						<tr>
							<td>Appointment Notes</td>
							<td>: $row->apptnotes</td>
						</tr>
						<tr>
							<td>Customer Notes(internal)</td>
							<td>: $row->customernotes</td>
						</tr>
						</tr>
							<tr>
							<td>Customer Instructions</td>
							<td>: $row->specialinstruction</td>
						</tr>


					 </table>
				
					 Please use the following url to view your appontment.  <a href='".$this->powered."/index1.php'>$this->powered/index1.php</a> ";



			$cusbody = $this->getBody($cusbody);
			$staffbody = $this->getBody($staffbody);

			$this->admin = $this->admin_email;
			if($update == 1) {
				$subject = "Your appointment has been updated : ";
			} else {
				$subject = "New Appointment created : ";
			}
			
			$this->sendMail($row->cusemail,$subject.$this->header,$cusbody);
			$this->sendMail($row->staffemail,$subject.$this->header,$staffbody);
		}

	}

	function createBusiness() {
	$query = "select bus.*,ad.username,ad.password from business_info as bus
			  left join administrator_info as ad on ad.business_id = bus.business_id
			  where bus.business_id = '$this->business_id'";
	
	$res = $this->query($query);
	$obj = mysql_fetch_object($res);			 
	
	$message="Dear $obj->firstname $obj->lastname ,<br /><br />";
	$message.= "Thank you for your interest in Online Appointment Manager Scheduling Services!<br />";
	$message.= "Your Online Appointment Manager account #$obj->business_id has been created.<br /><br />";
	$message.= "You can access your account at any time at:<br /><br />";
	
	$message.= $this->powered."/index1.php?business_id=".$obj->business_id." <br /><br />";
	
	
	$message.= "Once you complete your account setup wizard to define general settings,business hours,create services and staff records, your online scheduling pages
	will be automatically published at the URL listed below:<br /><br />";
	
	$message.= $this->powered."/index1.php?business_id=".$obj->business_id." <br /><br />";
	$message.= "For detailed instructions on how to integrate online appointment
	scheduling with your web site and where to direct your customers to
	make appointments, login to your Online Appointment Manager account
	and go to:<br /><br />";
	$message.= "Account - Setup Center - Web Site Integration<br /><br />";
	$message.= "ACCOUNT<br />";
	$message.= " Company Name: $obj->companyname <br />";
	$message.= " Account Number: $obj->business_id <br />";
	$message.= " Business Category: $obj->businesscategory <br />";
	
	$message.= "ACCOUNT ADMINISTRATOR<br />";
	$message.= "First Name: $obj->firstname <br />";
	$message.= "Last Name: $obj->lastname <br />";
	$message.= "username: $obj->username <br />";
	$message.= "Password: $obj->password <br /><br />";
	$message.= "Please, do not reply to this message.<br /><br />";
	
	$message = $this->getBody($message);

	$this->admin = $this->admin_email;
	
	$this->sendMail($obj->email,'Welcome '.$this->header,$message);

	}

}
?>